package com.ruoyi.scale.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.scale.domain.ScalePerson;
import com.ruoyi.scale.service.IScalePersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 人Controller
 *
 * @author dais
 * @date 2023-09-03
 */
@RestController
@RequestMapping("/scale/person")
public class ScalePersonController extends BaseController {
    @Autowired
    private IScalePersonService scalePersonService;

    /**
     * 查询人列表
     */
    @PreAuthorize("@ss.hasPermi('scale:person:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScalePerson scalePerson) {
        startPage();
        List<ScalePerson> list = scalePersonService.selectScalePersonList(scalePerson);
        return getDataTable(list);
    }

    /**
     * 导出人列表
     */
    @PreAuthorize("@ss.hasPermi('scale:person:export')")
    @Log(title = "人", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScalePerson scalePerson) {
        List<ScalePerson> list = scalePersonService.selectScalePersonList(scalePerson);
        ExcelUtil<ScalePerson> util = new ExcelUtil<ScalePerson>(ScalePerson.class);
        util.exportExcel(response, list, "人数据");
    }

    /**
     * 获取人详细信息
     */
    @PreAuthorize("@ss.hasPermi('scale:person:query')")
    @GetMapping(value = "/{personId}")
    public AjaxResult getInfo(@PathVariable("personId") Long personId) {
        return success(scalePersonService.selectScalePersonByPersonId(personId));
    }

    /**
     * 新增人
     */
    @PreAuthorize("@ss.hasPermi('scale:person:add')")
    @Log(title = "人", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScalePerson scalePerson) {
        return toAjax(scalePersonService.insertScalePerson(scalePerson));
    }

    /**
     * 修改人
     */
    @PreAuthorize("@ss.hasPermi('scale:person:edit')")
    @Log(title = "人", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScalePerson scalePerson) {
        return toAjax(scalePersonService.updateScalePerson(scalePerson));
    }

    /**
     * 删除人
     */
    @PreAuthorize("@ss.hasPermi('scale:person:remove')")
    @Log(title = "人", businessType = BusinessType.DELETE)
    @DeleteMapping("/{personIds}")
    public AjaxResult remove(@PathVariable Long[] personIds) {
        return toAjax(scalePersonService.deleteScalePersonByPersonIds(personIds));
    }
}
